// 系统信息工具函数
let systemInfo = null;

/**
 * 获取系统信息
 */
export const getSystemInfo = () => {
  if (systemInfo) {
    return systemInfo;
  }

  try {
    systemInfo = uni.getSystemInfoSync();
    return systemInfo;
  } catch (error) {
    console.error("获取系统信息失败:", error);
    return {};
  }
};

/**
 * 获取状态栏高度
 */
export const getStatusBarHeight = () => {
  const info = getSystemInfo();
  return info.statusBarHeight || 0;
};

/**
 * 获取标题栏高度
 */
export const getTitleBarHeight = () => {
  const info = getSystemInfo();

  // 不同平台的标题栏高度
  let titleBarHeight = 44; // 默认高度

  if (info.platform === "android") {
    titleBarHeight = 48;
  } else if (info.platform === "ios") {
    titleBarHeight = 44;
  }

  return titleBarHeight;
};

/**
 * 获取导航栏高度（状态栏 + 标题栏）
 */
export const getNavigationBarHeight = () => {
  const statusBarHeight = getStatusBarHeight();
  const titleBarHeight = getTitleBarHeight();
  return statusBarHeight + titleBarHeight;
};

/**
 * 获取安全区域信息
 */
export const getSafeAreaInsets = () => {
  const info = getSystemInfo();
  const safeArea = info.safeArea || {};
  const safeAreaInsets = info.safeAreaInsets || {};

  return {
    top: safeAreaInsets.top || getStatusBarHeight(),
    bottom: safeAreaInsets.bottom || 0,
    left: safeAreaInsets.left || 0,
    right: safeAreaInsets.right || 0,
    safeArea,
  };
};

/**
 * 获取屏幕信息
 */
export const getScreenInfo = () => {
  const info = getSystemInfo();
  return {
    screenWidth: info.screenWidth || 375,
    screenHeight: info.screenHeight || 667,
    windowWidth: info.windowWidth || 375,
    windowHeight: info.windowHeight || 667,
    pixelRatio: info.pixelRatio || 2,
  };
};

/**
 * rpx转px
 */
export const rpxToPx = (rpx) => {
  const { screenWidth } = getScreenInfo();
  return (rpx * screenWidth) / 750;
};

/**
 * px转rpx
 */
export const pxToRpx = (px) => {
  const { screenWidth } = getScreenInfo();
  return (px * 750) / screenWidth;
};
